/*
 * @Author: 李环玉 834654733@qq.com
 * @Date: 2023-12-20 19:07:05
 * @LastEditors: 李环玉 834654733@qq.com
 * @LastEditTime: 2024-01-07 22:18:37
 * @FilePath: \koa-server\src\service\statistics.service.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

const Menu = require('../model/menu.model')
const Blog = require('../model/blog.model')
const { Op } = require("sequelize")
class statisticsService {
  async getFirstNum(params){
    const json = await Menu.findAll({
      where:{ pId:null }
    })
    let data = {
      all:[],
      web:[],
      backEnd:[],
      database:[],
      other:[]
    }
    let arr = []
    for(let i =0;i< json.length;i++){
      let { count, rows } = await Blog.findAndCountAll({
        limit:10,
        offset:10,
        where:{
          firstClass:json[i].id
        }
      })
      arr.push({
        name:json[i].title,
        num:count
      })
    }
    data.all = arr
    for(let j = 0;j<json.length;j++){
      const jane =  await Menu.findAll({
        where:{ pId:json[j].id }
      })
      for(let k = 0;k<jane.length;k++){
        for(let key in data){
          if(key == json[j].type){
            let { count, rows } = await Blog.findAndCountAll({
              limit:10,
              offset:10,
              where:{
                secondClass:jane[k].id
              }
            })
            data[key].push({
              name:jane[k].title,
              num:count
            })
          }
        }
      }
    }
    return data
  }
}

module.exports = new statisticsService()